<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 意见反馈管理
 *
 * @icon fa fa-circle-o
 */
class Opinion extends Backend {

    /**
     * Opinion模型对象
     * @var \app\admin\model\Opinion
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\Opinion;
    }

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, [], ['user_id' => 'o.user_id']);
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->alias('o')
                    ->join('fa_users u', 'u.user_id = o.user_id', 'left')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function reply() {
        $id = input('id', 0);

        $reply = input('reply');


        if ($id == 0) {
            $this->error('请选择需要回复的反馈');
        }

        if (empty($reply)) {
            $this->error('请填写回复内容');
        }

        db('opinion')->where('opinion_id', $id)->update(['reply' => $reply, 'updatetime' => date('Y-m-d H:i:s')]);

        $this->success('回复成功');
    }

}
